#include <iostream>

#include "protonos8.hpp"

int
main(int argc, char* argv[])
{
	try
	{
		if (argc != 3) {
			std::cerr << "Usage: protonos8-test <host> <port>\n";
			return 1;
		}

		protonos8::client client(argv[1], argv[2]);

		uint32_t data;

		std::cout << "client.info: ack_result=" << (int)client.info(data) << "; ";
		std::cout << "protonos8 version info=0x" << std::hex << (int)data << std::dec << ".\n";

		uint8_t out[8];
		uint16_t dac[8];
		uint16_t adc[32];
		uint32_t cntrs[4];

		for (std::size_t i=0; i < 8; i+=2) {
			std::cout <<
				"client.init_m22[" << i <<
			       	"]: ack_result=" << (int)client.init_m22(i, 0) <<
			       	"\n";
			std::cout <<
				"client.init_m23[" << i <<
			       	"]: ack_result=" << (int)client.init_m23(i, 0) <<
			       	"\n";
			std::cout <<
				"client.init_m26[" << i <<
			       	"]: ack_result=" << (int)client.init_m26(i, 0) <<
			       	"\n";
			std::cout <<
				"client.init_m27[" << i <<
			       	"]: ack_result=" << (int)client.init_m27(i, 0) <<
			       	"\n";
			std::cout <<
				"client.init_m28[" << i <<
			       	"]: ack_result=" << (int)client.init_m28(i, 0) <<
			       	"\n";
			std::cout <<
				"client.init_m32[" << i <<
			       	"]: ack_result=" << (int)client.init_m32(i, 0) <<
			       	"\n";
			std::cout <<
				"client.init_m33[" << i <<
			       	"]: ack_result=" << (int)client.init_m33(i, 0) <<
			       	"\n";
			std::cout <<
				"client.init_m34[" << i <<
			       	"]: ack_result=" << (int)client.init_m34(i, 0) <<
			       	"\n";

			std::cout <<
				"client.read_m28[" << i <<
				"]: ack_result=" << (int)client.read_m28(i,0xff,cntrs) <<
				"\n";

			std::cout << "data=[" << std::hex << cntrs[0] <<
				"," << cntrs[1] <<
				"," << cntrs[2] <<
				"," << cntrs[3] <<
				std::dec <<
				"]\n";
			
			std::cout <<
				"client.read_m32[" << i <<
				"]: ack_result=" << (int)client.read_m32(i,0xff,cntrs) <<
				"\n";

			std::cout << "data=[" << std::hex << cntrs[0] <<
				"," << cntrs[1] <<
				"," << cntrs[2] <<
				"," << cntrs[3] <<
				std::dec <<
				"]\n";

			std::cout <<
				"client.read_m32[" << i <<
				"][2]: ack_result=" << (int)client.read_m32(i,2,cntrs) <<
				"\n";

			std::cout << "data=[" << std::hex << cntrs[0] <<
				"]\n";

			std::fill(dac, dac+8, 0xaaaa);

			std::cout <<
				"client.write_m26[" << i <<
				"]: ack_result=" << (int)client.write_m26(i,0xf,dac) <<
				"\n";

			std::cout << "data=[" << std::hex << dac[0] <<
				"," << dac[1] <<
				"," << dac[2] <<
				"," << dac[3] <<
				std::dec <<
				"]\n";

			std::cout <<
				"client.read_m22[" << i <<
				"]: ack_result=" << (int)client.read_m22(i,0xffff,adc) <<
				"\n";

			std::cout << "data=[" << std::hex << adc[0] <<
				"," << adc[1] <<
				"," << adc[2] <<
				"," << adc[3] <<
				"," << adc[4] <<
				"," << adc[5] <<
				"," << adc[6] <<
				"," << adc[7] <<
				"," << adc[8] <<
				"," << adc[9] <<
				"," << adc[10] <<
				"," << adc[11] <<
				"," << adc[12] <<
				"," << adc[13] <<
				"," << adc[14] <<
				"," << adc[15] <<
				std::dec <<
				"]\n";

			std::fill(dac, dac+8, 0x5555);

			std::cout <<
				"client.write_m27[" << i <<
				"]: ack_result=" << (int)client.write_m27(i,0xa,dac) <<
				"\n";

			std::cout << "data=[" << std::hex << dac[0] <<
				"," << dac[1] <<
				"," << dac[2] <<
				"," << dac[3] <<
				std::dec <<
				"]\n";

			std::cout <<
				"client.read_m23[" << i <<
				"]: ack_result=" << (int)client.read_m23(i,0xffff,adc) <<
				"\n";

			std::cout << "data=[" << std::hex << adc[0] <<
				"," << adc[1] <<
				"," << adc[2] <<
				"," << adc[3] <<
				"," << adc[4] <<
				"," << adc[5] <<
				"," << adc[6] <<
				"," << adc[7] <<
				"," << adc[8] <<
				"," << adc[9] <<
				"," << adc[10] <<
				"," << adc[11] <<
				"," << adc[12] <<
				"," << adc[13] <<
				"," << adc[14] <<
				"," << adc[15] <<
				std::dec <<
				"]\n";

			std::cout <<
				"client.read_m33[" << i <<
				"]: ack_result=" << (int)client.read_m33(i,0xffff,adc) <<
				"\n";

			std::cout << "data=[" << std::hex << adc[0] <<
				"," << adc[1] <<
				"," << adc[2] <<
				"," << adc[3] <<
				"," << adc[4] <<
				"," << adc[5] <<
				"," << adc[6] <<
				"," << adc[7] <<
				"," << adc[8] <<
				"," << adc[9] <<
				"," << adc[10] <<
				"," << adc[11] <<
				"," << adc[12] <<
				"," << adc[13] <<
				"," << adc[14] <<
				"," << adc[15] <<
				std::dec <<
				"]\n";

			std::cout <<
				"client.read_m34[" << i <<
				"]: ack_result=" << (int)client.read_m34(i,0xffff,adc) <<
				"\n";

			std::cout << "data=[" << std::hex << adc[0] <<
				"," << adc[1] <<
				"," << adc[2] <<
				"," << adc[3] <<
				"," << adc[4] <<
				"," << adc[5] <<
				"," << adc[6] <<
				"," << adc[7] <<
				"," << adc[8] <<
				"," << adc[9] <<
				"," << adc[10] <<
				"," << adc[11] <<
				"," << adc[12] <<
				"," << adc[13] <<
				"," << adc[14] <<
				"," << adc[15] <<
				std::dec <<
				"]\n";
		}
		std::cout <<
			"client.init_mmc01: ack_result=" << (int)client.init_mmc01(0) <<
			"\n";
		std::cout <<
			"client.init_mmc03: ack_result=" << (int)client.init_mmc03(0) <<
			"\n";
		std::cout <<
			"client.init_mmc06: ack_result=" << (int)client.init_mmc06(0) <<
			"\n";

		std::fill(out, out+8, 0x55);

		std::cout <<
			"client.write_mmc01: ack_result=" << (int)client.write_mmc01(out) <<
			"\n";


		dac[0] = 0x1111;
		dac[1] = 0x2222;
		dac[2] = 0x3333;
		dac[3] = 0x4444;
		dac[4] = 0x5555;
		dac[5] = 0x5555;
		dac[6] = 0x6666;
		dac[7] = 0x7777;

		std::cout <<
			"client.rw_mmc03: ack_result=" << (int)client.rw_mmc03(0xff, dac, 0xffffffff, adc) <<
			"\n";

		std::cout << "data=[" << std::hex << dac[0] <<
			"," << dac[1] <<
			"," << dac[2] <<
			"," << dac[3] <<
			"," << dac[4] <<
			"," << dac[5] <<
			"," << dac[6] <<
			"," << dac[7] <<
			std::dec <<
			"]\n";

		std::cout << "data=[" << std::hex << adc[0] <<
			"," << adc[1] <<
			"," << adc[2] <<
			"," << adc[3] <<
			"," << adc[4] <<
			"," << adc[5] <<
			"," << adc[6] <<
			"," << adc[7] <<
			"," << adc[8] <<
			"," << adc[9] <<
			"," << adc[10] <<
			"," << adc[11] <<
			"," << adc[12] <<
			"," << adc[13] <<
			"," << adc[14] <<
			"," << adc[15] <<
			std::dec <<
			"]\n";
		std::cout << "data=[" << std::hex << adc[16] <<
			"," << adc[17] <<
			"," << adc[18] <<
			"," << adc[19] <<
			"," << adc[20] <<
			"," << adc[21] <<
			"," << adc[22] <<
			"," << adc[23] <<
			"," << adc[24] <<
			"," << adc[25] <<
			"," << adc[26] <<
			"," << adc[27] <<
			"," << adc[28] <<
			"," << adc[29] <<
			"," << adc[30] <<
			"," << adc[31] <<
			std::dec <<
			"]\n";

		dac[7] = 0x1111;
		dac[6] = 0x2222;
		dac[5] = 0x3333;
		dac[4] = 0x4444;
		dac[3] = 0x5555;
		dac[2] = 0x5555;
		dac[1] = 0x6666;
		dac[0] = 0x7777;

		std::cout <<
			"client.rw_mmc06: ack_result=" << (int)client.rw_mmc06(0xff, dac, 0xffffffff, adc) <<
			"\n";

		std::cout << "data=[" << std::hex << dac[0] <<
			"," << dac[1] <<
			"," << dac[2] <<
			"," << dac[3] <<
			"," << dac[4] <<
			"," << dac[5] <<
			"," << dac[6] <<
			"," << dac[7] <<
			std::dec <<
			"]\n";


		std::cout << "data=[" << std::hex << adc[0] <<
			"," << adc[1] <<
			"," << adc[2] <<
			"," << adc[3] <<
			"," << adc[4] <<
			"," << adc[5] <<
			"," << adc[6] <<
			"," << adc[7] <<
			"," << adc[8] <<
			"," << adc[9] <<
			"," << adc[10] <<
			"," << adc[11] <<
			"," << adc[12] <<
			"," << adc[13] <<
			"," << adc[14] <<
			"," << adc[15] <<
			std::dec <<
			"]\n";
		std::cout << "data=[" << std::hex << adc[16] <<
			"," << adc[17] <<
			"," << adc[18] <<
			"," << adc[19] <<
			"," << adc[20] <<
			"," << adc[21] <<
			"," << adc[22] <<
			"," << adc[23] <<
			"," << adc[24] <<
			"," << adc[25] <<
			"," << adc[26] <<
			"," << adc[27] <<
			"," << adc[28] <<
			"," << adc[29] <<
			"," << adc[30] <<
			"," << adc[31] <<
			std::dec <<
			"]\n";

	} catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}

	return 0;
}
